﻿Imports Microsoft.Practices.EnterpriseLibrary.Common.Configuration
Imports System.Data.Common
Public Class PermisoData
#Region "Declaracion"
    Private _database As Database
#End Region

#Region "Constructor"
    Public Sub New()
        _database = EnterpriseLibraryContainer.Current.GetInstance(Of Database)("Vinum")
    End Sub
#End Region

#Region "Funciones"
    Public Function ConsultarPermisos(ByVal Role As ClaseEnumObj.Roles, ByVal UsuarioActual As UsuarioObj) As PermisoObj
        Using Transaccion As New System.Transactions.TransactionScope
            Try
                Dim PermisoRetorno As New PermisoObj

                Dim Comando As DbCommand = _database.GetStoredProcCommand("PaConsultarPermisos")

                _database.AddInParameter(Comando, "Opcion", DbType.Int32, 0)
                _database.AddInParameter(Comando, "IdRol", DbType.Int32, Role)
                _database.AddInParameter(Comando, "IdUsuario", DbType.Int32, UsuarioActual.Id)

                _database.AddOutParameter(Comando, "Visualizar", DbType.Boolean, 1)
                _database.AddOutParameter(Comando, "Incluir", DbType.Boolean, 1)
                _database.AddOutParameter(Comando, "Editar", DbType.Boolean, 1)
                _database.AddOutParameter(Comando, "Eliminar", DbType.Boolean, 1)

                _database.ExecuteNonQuery(Comando)

                PermisoRetorno.Visualizar = Convert.ToBoolean(_database.GetParameterValue(Comando, "Visualizar"))
                PermisoRetorno.Incluir = Convert.ToBoolean(_database.GetParameterValue(Comando, "Incluir"))
                PermisoRetorno.Editar = Convert.ToBoolean(_database.GetParameterValue(Comando, "Editar"))
                PermisoRetorno.Eliminar = Convert.ToBoolean(_database.GetParameterValue(Comando, "Eliminar"))

                Return PermisoRetorno

                Transaccion.Complete()

            Catch ex As SqlException
                Throw ex
            Catch ex As Exception
                Throw New AccesoDatosException(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), ex)
            End Try
        End Using
    End Function
#End Region
End Class
